package com.wt.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.order.common.CustomException;
import com.wt.order.dto.DishDto;
import com.wt.order.dto.SetmealDto;
import com.wt.order.entity.Setmeal;
import com.wt.order.entity.SetmealDish;
import com.wt.order.mapper.SetmealMapper;
import com.wt.order.service.SetmealDishService;
import com.wt.order.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 吴涛
 * @date 2022/5/11 11:13
 * @qq 337144203
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增加套餐，并保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息
        this.save(setmealDto);
        //保存套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((wt) -> {
            wt.setSetmealId(setmealDto.getId());
            return wt;
        }).collect(Collectors.toList());


        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void removeWithDish(List<Long> ids) {
        //停售的才能删除，查询订单状态是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            //说明查出来了数据，不能删除 则抛出一个业务异常，当前套餐正在售卖中，不能删除
            throw new CustomException("套餐正在售卖中，无法删除");
        }
        //若可以删除，则先删除套餐表中的数据Setmeals
        this.removeByIds(ids);

        //再来删除关系表中的数据setmeal_dish表
        //delete from setmeal_dish where setmeal_id in (?,?,?);
        /**
         * 批量删除了
         */
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(dishLambdaQueryWrapper);
    }


    /**
     * 根据id查询套餐信息和其对应的菜品信息
     * @param id
     * @return
     */
    public SetmealDto getByIdWithDish(Long id) {
        //从Setmeal中查出基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //使用对象深拷贝将setemal拷贝到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);

        //构建查询构造器,从SetmealDish查找当前套餐对应的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //查询当前套餐对应的菜品
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }


    @Transactional
    public void updateWithDish(SetmealDto setmealDto){
        this.updateById(setmealDto);

        //清理当前套餐对应的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //添加当前页面提交过来的数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((wt)->{
            wt.setSetmealId(setmealDto.getId());
            return wt;
        }).collect(Collectors.toList());
        //批量保存
        setmealDishService.saveBatch(setmealDishes);

    }

}
